package com.china08.yxyapi.api.manage.tec.teaching.lecture.catalog;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLectureCatalog;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.lecture.YxyLectureCatalogRepository;

/**
 * 老师 ---备授课--备课目录 当返回的目录结构为null或者空，需要跳转到完善信息页面
 * 
 */
@CrossOrigin
@RestController
public class LectureSrhCatalogController extends YxyApiBaseController {

    @Autowired
    private YxyLectureCatalogRepository yxyLectureCatalogRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/catalog/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response) {

        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();

        Assert.notNull(StringUtils.defaultIfBlank(userId, null), "用户id必须");
        Assert.notNull(StringUtils.defaultIfBlank(schoolId, null), "学校id必须");

        List<YxyLectureCatalog> catalogs = yxyLectureCatalogRepository
                .findByUserIdAndSchoolIdAndStatusOrderByIsDefaultDesc(userId, schoolId, 1);
        List<RespModel> respModels = new ArrayList<RespModel>();
        if (catalogs != null && !catalogs.isEmpty()) {
            for (YxyLectureCatalog catalog : catalogs) {
                RespModel respModel = new RespModel();
                respModel.phaseName = catalog.getPhaseName();
                respModel.subjectName = catalog.getSubjectName();
                respModel.versionName = catalog.getVersionName();
                respModel.semesterName = catalog.getSemesterName();
                respModel.isDefault = catalog.getIsDefault();
                respModel.bookId = catalog.getBookId();
                respModel.id = catalog.getId();
                respModel.phaseId = catalog.getPhaseId();
                respModel.subjectId = catalog.getSubjectId();
                respModels.add(respModel);
            }
        }
        return respModels;
    }

    protected static class RespModel extends YxyApiRespModel {
        public String id;

        public String phaseName;

        public String subjectName;

        public String versionName;

        public String semesterName;

        public Integer isDefault;

        public String bookId;

        public String phaseId;

        public String subjectId;

    }
}
